//
//  MemberTopTableViewCell.m
//  Cat
//
//  Created by lushitong on 2018/12/1.
//  Copyright © 2018年 lushitong. All rights reserved.
//

#import "MemberTopTableViewCell.h"
#import "MemberModel.h"
#import "FLHeadView.h"

@interface MemberTopTableViewCell ()

@property (nonatomic, strong) UIImageView *topBackImageView;
@property (nonatomic, strong) UIImageView *operView;
@property (nonatomic, strong) FLHeadView *headView;
@property (nonatomic, strong) UILabel *numberLabel;
@property (nonatomic, strong) UIImageView *vipImageView;
@property (nonatomic, strong) UIProgressView *progressView;
@property (nonatomic, strong) UILabel *leftInfoLabel;
@property (nonatomic, strong) UILabel *rightLabel;

@property (nonatomic, strong) UIImageView *bordImage;
@property (nonatomic, strong) UILabel *centerLabel;

@end

@implementation MemberTopTableViewCell

- (void)setCellModelData:(id)cellModelData {
	MemberModel *model = (MemberModel *)cellModelData;
	[self.headView setImageUrl:[NSURL URLWithString:model.avatar] size:CGSizeMake(kQKFitModuleDecimal(40), kQKFitModuleDecimal(40)) cornerRadius:kQKFitModuleDecimal(20)];
	self.numberLabel.text = model.nickName;
	[self.progressView setProgress:model.inviteNum.floatValue / model.upgradeNum.floatValue animated:YES];
	self.centerLabel.text = [NSString stringWithFormat:@"%@/%@升级",[model.inviteNum.stringValue validateString] ? model.inviteNum.stringValue :@"0",model.upgradeNum.stringValue];

	// FIXME:需要重新布局
	[self.bordImage mas_updateConstraints:^(MASConstraintMaker *make) {
		make.left.equalTo(self.progressView.left).offset((ScreenWidth - kQKFitModuleDecimal(32)) * (model.rank.floatValue / model.upgradeNum.floatValue));
	}];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
	self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
	if (self) {
		self.cellSeparatorStyle = QKUITableViewCellSeparatorStyleNone;
		self.selectionStyle = UITableViewCellSelectionStyleNone;
		[self loadSubviews];
	}
	return self;
}

- (void)loadSubviews {
	[self.contentView addSubviews:@[self.topBackImageView,self.operView
									]];
	
	[self.operView addSubviews:@[self.headView,
								 self.numberLabel,
								 self.vipImageView,
								 self.progressView,
								 self.leftInfoLabel,
								 self.rightLabel,
								 self.bordImage,
								 self.centerLabel]];
	
	[self.topBackImageView mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.and.right.and.top.equalTo(self.contentView);
		make.height.equalTo(@(kQKFitModuleDecimal(195)));
	}];
	
	[self.operView mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.equalTo(self.contentView.mas_left).offset(kQKFitModuleDecimal(16));
		make.right.equalTo(self.contentView.mas_right).offset(-kQKFitModuleDecimal(16));
		make.top.equalTo(self.contentView.mas_top).offset(kQKFitModuleDecimal(80));
		make.height.equalTo(@(kQKFitModuleDecimal(134)));
	}];
	
	[self.headView mas_makeConstraints:^(MASConstraintMaker *make) {
		make.size.equalTo(CGSizeMake(kQKFitModuleDecimal(40), kQKFitModuleDecimal(40)));
		make.top.equalTo(self.operView.mas_top).offset(kQKFitModuleDecimal(16));
		make.left.equalTo(self.operView.mas_left).offset(kQKFitModuleDecimal(16));
	}];
	
	[self.numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.equalTo(self.headView.mas_right).offset(kQKFitModuleDecimal(10));
		make.top.equalTo(self.operView.mas_top).offset(kQKFitModuleDecimal(23));
	}];
	
	[self.vipImageView mas_makeConstraints:^(MASConstraintMaker *make) {
		make.size.equalTo(CGSizeMake(kQKFitModuleDecimal(60), kQKFitModuleDecimal(16)));
		make.left.equalTo(self.numberLabel.mas_right).offset(kQKFitModuleDecimal(5));
		make.centerY.equalTo(self.numberLabel.mas_centerY);
	}];
	
	[self.progressView mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.equalTo(self.operView.mas_left).offset(kQKFitModuleDecimal(16));
		make.right.equalTo(self.operView.mas_right).offset(-kQKFitModuleDecimal(16));
		make.top.equalTo(self.headView.mas_bottom).offset(kQKFitModuleDecimal(32));
		make.height.equalTo(@(kQKFitModuleDecimal(6)));
	}];
	
	[self.leftInfoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.equalTo(self.progressView.mas_left);
		make.top.equalTo(self.progressView.mas_bottom).offset(kQKFitModuleDecimal(8));
	}];
	
	[self.rightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
		make.right.equalTo(self.progressView.mas_right);
		make.top.equalTo(self.progressView.mas_bottom).offset(kQKFitModuleDecimal(8));
	}];
	
	[self.bordImage mas_makeConstraints:^(MASConstraintMaker *make) {
		make.size.equalTo(CGSizeMake(kQKFitModuleDecimal(61), kQKFitModuleDecimal(17)));
		make.bottom.equalTo(self.progressView.mas_top).offset(-kQKFitModuleDecimal(3));
		make.left.equalTo(self.progressView.mas_left);
	}];
	
	[self.centerLabel mas_makeConstraints:^(MASConstraintMaker *make) {
		make.top.equalTo(self.bordImage.mas_top).offset(kQKFitModuleDecimal(3));
		make.centerX.equalTo(self.bordImage.mas_centerX);
	}];
}


- (void)updateCoverWithOffset:(CGFloat)offset {
	self.topBackImageView.y = offset;
	self.topBackImageView.height = kQKFitModule(195) + fabs(offset) + StatusBarSafeInsetHeight;
}

#pragma mark----- lazy init -----

- (UIImageView *)topBackImageView {
	if (!_topBackImageView) {
		_topBackImageView = [[UIImageView alloc] init];
		_topBackImageView.backgroundColor = COLOR_WITH_HEX(0xFFC236);
	}
	return _topBackImageView;
}

- (UIImageView *)operView {
	if (!_operView) {
		_operView = [[UIImageView alloc] init];
		_operView.image = [UIImage createImageWithSize:CGSizeMake(ScreenWidth - 2 * kQKFitModuleDecimal(30), kQKFitModuleDecimal(124)) gradientColors:@[COLOR_WITH_HEX(0x515359),COLOR_WITH_HEX(0x262628)] percentage:@[@(0),@(1)] gradientType:GradientFromLeftToRight];
		_operView.layer.cornerRadius = kQKFitModuleDecimal(6);
		_operView.layer.masksToBounds = YES;
	}
	return _operView;
}

- (FLHeadView *)headView {
	if (!_headView) {
		_headView = [[FLHeadView alloc] init];
		
	}
	return _headView;
}

- (UILabel *)numberLabel {
	if (!_numberLabel) {
		_numberLabel = [UILabel labelWithFont:[UIFont systemFontOfSize:kQKFitFont(21)] textColor:COLOR_WITH_HEX(0xCDC7C4)];
		_numberLabel.text = @"13543567890";
	}
	return _numberLabel;
}

- (UIImageView *)vipImageView {
	if (!_vipImageView) {
		_vipImageView = [[UIImageView alloc] init];
		_vipImageView.image = [UIImage imageNamed:@"me_vip_pic"];
	}
	return _vipImageView;
}

- (UIProgressView *)progressView {
	if (!_progressView) {
		_progressView = [[UIProgressView alloc] init];
		_progressView.layer.cornerRadius = kQKFitModuleDecimal(3);
		_progressView.layer.masksToBounds = YES;
		_progressView.trackTintColor= COLOR_WITH_HEX(0x2A2A2A);
		_progressView.progressTintColor = COLOR_WITH_HEX(0xE1B75C);
	}
	return _progressView;
}

- (UILabel *)leftInfoLabel {
	if (!_leftInfoLabel) {
		_leftInfoLabel = [UILabel labelWithText:@"普通会员" font:[UIFont systemFontOfSize:kQKFitFont(10)] textColor:COLOR_WITH_HEX(0xCDC7C4)];
	}
	return _leftInfoLabel;
}

- (UILabel *)rightLabel {
	if (!_rightLabel) {
		_rightLabel = [UILabel labelWithText:@"超级合伙人" font:[UIFont systemFontOfSize:kQKFitFont(10)] textColor:COLOR_WITH_HEX(0xCDC7C4)];
	}
	return _rightLabel;
}

- (UIImageView *)bordImage {
	if (!_bordImage) {
		_bordImage = [[UIImageView alloc] init];
		_bordImage.image = [UIImage imageNamed:@"member_bord"];
	}
	return _bordImage;
}

- (UILabel *)centerLabel {
	if (!_centerLabel) {
		_centerLabel = [UILabel labelWithText:@"12/300升级" font:[UIFont systemFontOfSize:kQKFitFont(9)] textColor:COLOR_WITH_HEX(0xCDC7C4)];
	}
	return _centerLabel;
}

@end
